/*
MyOpenLab by Carmelo Salafia www.myopenlab.de
Copyright (C) 2004  Carmelo Salafia cswi@gmx.de

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/


package codeeditor;

import java.io.File;

import sun.tools.jar.*;
import VisualLogic.*;
import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JLabel;
import javax.swing.JPanel;

/**
 *
 * @author  Homer
 */
public class frmCodeEditor extends javax.swing.JFrame
{
    
    private String elementPath;
    private String elementLocation;
    private String filename;
    
    
    /** Creates new form frmCodeEditor */
    public frmCodeEditor(FrameMain parent)
    {
        this.initComponents();
        try
        {
            setIconImage(parent.iconImage);
        }
        catch(Exception ex)
        { }
        
    }
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
    private void initComponents() {
        jPanel1 = new javax.swing.JPanel();
        jToolBar1 = new javax.swing.JToolBar();
        jButton4 = new javax.swing.JButton();
        jButton3 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();
        jTabbedPane1 = new javax.swing.JTabbedPane();
        jMenuBar2 = new javax.swing.JMenuBar();
        jmnuFile = new javax.swing.JMenu();
        jmniLoad = new javax.swing.JMenuItem();
        jmniSaveProject = new javax.swing.JMenuItem();
        jmniClose = new javax.swing.JMenuItem();
        jmnuEdit = new javax.swing.JMenu();
        jmniUndo = new javax.swing.JMenuItem();
        jmnuRedo = new javax.swing.JMenuItem();
        jSeparator1 = new javax.swing.JSeparator();
        jmniCut = new javax.swing.JMenuItem();
        jmniCopy = new javax.swing.JMenuItem();
        jmniPaste = new javax.swing.JMenuItem();
        jSeparator2 = new javax.swing.JSeparator();
        jmniSearch = new javax.swing.JMenuItem();

        setDefaultCloseOperation(javax.swing.WindowConstants.DO_NOTHING_ON_CLOSE);
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosing(java.awt.event.WindowEvent evt) {
                formWindowClosing(evt);
            }
        });

        jPanel1.setLayout(new java.awt.BorderLayout());

        jToolBar1.setFloatable(false);
        jToolBar1.setRollover(true);
        jButton4.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Bilder/open16.gif")));
        java.util.ResourceBundle bundle = java.util.ResourceBundle.getBundle("codeeditor/frmCodeEditor"); // NOI18N
        jButton4.setText(bundle.getString("Load_File")); // NOI18N
        jButton4.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton4ActionPerformed(evt);
            }
        });

        jToolBar1.add(jButton4);

        jButton3.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Bilder/save16.gif")));
        jButton3.setText(bundle.getString("save_Project")); // NOI18N
        jButton3.setToolTipText("Save");
        jButton3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton3ActionPerformed(evt);
            }
        });

        jToolBar1.add(jButton3);

        jButton2.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Bilder/compile16.gif")));
        jButton2.setText(bundle.getString("Compile")); // NOI18N
        jButton2.setHorizontalAlignment(javax.swing.SwingConstants.LEFT);
        jButton2.setPreferredSize(new java.awt.Dimension(120, 25));
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        jToolBar1.add(jButton2);

        jPanel1.add(jToolBar1, java.awt.BorderLayout.CENTER);

        getContentPane().add(jPanel1, java.awt.BorderLayout.NORTH);

        getContentPane().add(jTabbedPane1, java.awt.BorderLayout.CENTER);

        jmnuFile.setText(bundle.getString("File")); // NOI18N
        jmnuFile.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jmnuFileActionPerformed(evt);
            }
        });

        jmniLoad.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Bilder/open16.gif")));
        jmniLoad.setText(bundle.getString("Load_File")); // NOI18N
        jmnuFile.add(jmniLoad);

        jmniSaveProject.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Bilder/save16.gif")));
        jmniSaveProject.setText(bundle.getString("save_Project")); // NOI18N
        jmnuFile.add(jmniSaveProject);

        jmniClose.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Bilder/16x16/system-log-out.png")));
        jmniClose.setText(bundle.getString("Close")); // NOI18N
        jmniClose.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jmniCloseActionPerformed(evt);
            }
        });

        jmnuFile.add(jmniClose);

        jMenuBar2.add(jmnuFile);

        jmnuEdit.setText(bundle.getString("Edit")); // NOI18N
        jmniUndo.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_Z, java.awt.event.InputEvent.CTRL_MASK));
        jmniUndo.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Bilder/16x16/edit-undo.png")));
        jmniUndo.setText(bundle.getString("Undo")); // NOI18N
        jmniUndo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jmniUndoActionPerformed(evt);
            }
        });

        jmnuEdit.add(jmniUndo);

        jmnuRedo.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_Y, java.awt.event.InputEvent.CTRL_MASK));
        jmnuRedo.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Bilder/16x16/edit-redo.png")));
        jmnuRedo.setText(bundle.getString("Redo")); // NOI18N
        jmnuRedo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jmnuRedoActionPerformed(evt);
            }
        });

        jmnuEdit.add(jmnuRedo);

        jmnuEdit.add(jSeparator1);

        jmniCut.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_X, java.awt.event.InputEvent.CTRL_MASK));
        jmniCut.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Bilder/16x16/edit-cut.png")));
        jmniCut.setText(bundle.getString("Cut")); // NOI18N
        jmniCut.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jmniCutActionPerformed(evt);
            }
        });

        jmnuEdit.add(jmniCut);

        jmniCopy.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_C, java.awt.event.InputEvent.CTRL_MASK));
        jmniCopy.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Bilder/16x16/edit-copy.png")));
        jmniCopy.setText(bundle.getString("Copy")); // NOI18N
        jmniCopy.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jmniCopyActionPerformed(evt);
            }
        });

        jmnuEdit.add(jmniCopy);

        jmniPaste.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_V, java.awt.event.InputEvent.CTRL_MASK));
        jmniPaste.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Bilder/16x16/edit-paste.png")));
        jmniPaste.setText(bundle.getString("Paste")); // NOI18N
        jmniPaste.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jmniPasteActionPerformed(evt);
            }
        });

        jmnuEdit.add(jmniPaste);

        jmnuEdit.add(jSeparator2);

        jmniSearch.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_F, java.awt.event.InputEvent.CTRL_MASK));
        jmniSearch.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Bilder/16x16/system-search.png")));
        jmniSearch.setText(bundle.getString("Search")); // NOI18N
        jmniSearch.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jmniSearchActionPerformed(evt);
            }
        });

        jmnuEdit.add(jmniSearch);

        jMenuBar2.add(jmnuEdit);

        setJMenuBar(jMenuBar2);

        java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
        setBounds((screenSize.width-631)/2, (screenSize.height-526)/2, 631, 526);
    }// </editor-fold>//GEN-END:initComponents

    private void jmniCloseActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jmniCloseActionPerformed
        formWindowClosing(null);
    }//GEN-LAST:event_jmniCloseActionPerformed

    private void jmniPasteActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jmniPasteActionPerformed
        PanelEditor panel = getAktuellerEditor();
        if (panel!=null)
        {
            panel.paste();
        }

    }//GEN-LAST:event_jmniPasteActionPerformed

    private void jmniCopyActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jmniCopyActionPerformed
        PanelEditor panel = getAktuellerEditor();
        if (panel!=null)
        {
            panel.copy();
        }

    }//GEN-LAST:event_jmniCopyActionPerformed

    private void jmniCutActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jmniCutActionPerformed

        PanelEditor panel = getAktuellerEditor();
        if (panel!=null)
        {
            panel.cut();
        }
        
    }//GEN-LAST:event_jmniCutActionPerformed

    private void jmnuRedoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jmnuRedoActionPerformed
        PanelEditor panel = getAktuellerEditor();
        if (panel!=null)
        {
            panel.redo();
        }

    }//GEN-LAST:event_jmnuRedoActionPerformed

    
    public PanelEditor getAktuellerEditor()
    {
        if (jTabbedPane1.getSelectedComponent() instanceof PanelEditor)
        {
            return (PanelEditor)jTabbedPane1.getSelectedComponent();
        }
        
        return null;
    }
    
    
    private void jmniSearchActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jmniSearchActionPerformed

       
        PanelEditor panel = getAktuellerEditor();
        if (panel!=null)
        {
            panel.openSearchDialog();
        }
        
    }//GEN-LAST:event_jmniSearchActionPerformed

    private void jmniUndoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jmniUndoActionPerformed

        PanelEditor panel = getAktuellerEditor();
        if (panel!=null)
        {
            panel.undo();
        }
        
    }//GEN-LAST:event_jmniUndoActionPerformed

    private void jmnuFileActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jmnuFileActionPerformed
// TODO add your handling code here:
    }//GEN-LAST:event_jmnuFileActionPerformed
    
    private void formWindowClosing(java.awt.event.WindowEvent evt)//GEN-FIRST:event_formWindowClosing
    {//GEN-HEADEREND:event_formWindowClosing
        
        for (int i=0;i<jTabbedPane1.getTabCount();i++)
        {
            Component comp= jTabbedPane1.getComponentAt(i);
            
            if (comp instanceof PanelEditor)
            {
                PanelEditor editor = (PanelEditor)comp;
                if (editor.onClose()==false) return;
            }
        }
        dispose();
    }//GEN-LAST:event_formWindowClosing
    
    
    public boolean openFile()
    {
        JFileChooser chooser  = new JFileChooser();
        String make=elementPath+"/src";
        chooser.setCurrentDirectory(new java.io.File(make));
        
        chooser.addChoosableFileFilter(new javax.swing.filechooser.FileFilter()
        {
            public boolean accept(File f)
            {
                if (f.isDirectory()) return true;
                return f.getName().toLowerCase().endsWith(".java");
            }
            public String getDescription()
            { return "Java Files"; }
        });
        int returnVal = chooser.showOpenDialog(this);
        
        if(returnVal == JFileChooser.APPROVE_OPTION)
        {
            loadFile(chooser.getSelectedFile().getAbsolutePath());
            
            return true;
        }
        else return false;
    }
    
    private void jButton4ActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_jButton4ActionPerformed
    {//GEN-HEADEREND:event_jButton4ActionPerformed
        openFile();
    }//GEN-LAST:event_jButton4ActionPerformed
    
    private void saveProject()
    {
        for (int i=0;i<jTabbedPane1.getTabCount();i++)
        {
            Component comp = jTabbedPane1.getComponentAt(i);
            if (comp instanceof PanelEditor)
            {
                PanelEditor editor = (PanelEditor)comp;
                
                editor.saveFile();
            }
        }
    }
    
    
    private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton3ActionPerformed
        saveProject();
    }//GEN-LAST:event_jButton3ActionPerformed
    

    
    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
                        
        saveProject();
        
        for (int i=0;i<jTabbedPane1.getTabCount();i++)
        {
            Component comp = jTabbedPane1.getComponentAt(i);
            if (comp instanceof PanelEditor)
            {
                PanelEditor editor = (PanelEditor)comp;
                
                String srcFile=editor.filename;

                File file= new File(elementLocation);
                DFProperties definition_def =Tools.getProertiesFromDefinitionFile(file);
                                
                Tools.compileFile(elementPath, srcFile,elementLocation, definition_def.classPath);
            }
        }

    }//GEN-LAST:event_jButton2ActionPerformed
    
    public void execute(String elementPath, String elementLocation, String elementname)
    {
        this.elementPath=elementPath;
        this.elementLocation=elementLocation;
        
        
        File file= new File(elementLocation);
        DFProperties definition_def =Tools.getProertiesFromDefinitionFile(file);
        
        String fn=elementLocation+"/src/";
        
        String srcPath=new File(fn).getAbsolutePath()+"/";
        
        jTabbedPane1.removeAll();
        if (definition_def.classcircuit.length()>0)
        {
            loadFile(srcPath+definition_def.classcircuit+".java");
        }
        
        if (definition_def.classfront.length()>0)
        {
            loadFile(srcPath+definition_def.classfront+".java");
        }
        
        setTitle(java.util.ResourceBundle.getBundle("codeeditor/frmCodeEditor").getString("Code_Editor")+" ["+ elementname+"]");
        setVisible(true);
    }
    
    
    class JButtonX extends JButton
    {
        public PanelEditor panel;
        
        public JButtonX(javax.swing.ImageIcon icon)
        {
            super(icon);
        }
    }
    
    public void loadFile(String filename)
    {
        PanelEditor pnl = new PanelEditor(this);
        pnl.loadFile(filename);
        
        //jTabbedPane1.add(panel,new File(filename).getName());
        
        //jTabbedPane1.setSelectedComponent(pnl);
        
        String caption=new File(filename).getName();
        jTabbedPane1.add(pnl);
                        
        final JPanel content = new JPanel();
        
        JPanel tab = new JPanel();
        tab.setLayout(new BorderLayout());
        tab.setOpaque(false);
        
        JLabel tabLabel = new JLabel();
        
        tabLabel.setText(caption+"  ");
                
        javax.swing.ImageIcon closeXIcon=new javax.swing.ImageIcon(getClass().getResource("/Bilder/Cross9x9.png"));
        Dimension closeButtonSize;
        
        closeButtonSize = new Dimension(closeXIcon.getIconWidth()+3,closeXIcon.getIconHeight()+3);
        
        JButtonX tabCloseButton = new JButtonX(closeXIcon);
        tabCloseButton.setBorderPainted(false);
        tabCloseButton.panel=pnl;
        
        tabCloseButton.setPreferredSize(closeButtonSize);
                
        ImageIcon icon = new ImageIcon(getClass().getResource("/Bilder/16x16/page_java.gif"));
        tabLabel.setIcon(icon);
        tabLabel.setBorder(null);
        tab.setBorder(null);
        tabCloseButton.setBorder(null);
        
        tabCloseButton.addActionListener(new ActionListener()
        {
            public void actionPerformed(ActionEvent e)
            {
                if (e.getSource() instanceof JButtonX)
                {                    
                    JButtonX btn = (JButtonX)e.getSource();
                    
                    btn.panel.close();                                                
                }
            }
        });
        
        tab.setBorder(javax.swing.BorderFactory.createEmptyBorder(1, 1, 1, 1));
        
        tab.add(tabLabel, BorderLayout.WEST);
        tab.add(tabCloseButton, BorderLayout.EAST);
        
        jTabbedPane1.setTabComponentAt(jTabbedPane1.getTabCount()-1, tab);
        
    }
    
    
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton2;
    private javax.swing.JButton jButton3;
    private javax.swing.JButton jButton4;
    private javax.swing.JMenuBar jMenuBar2;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JSeparator jSeparator2;
    private javax.swing.JTabbedPane jTabbedPane1;
    private javax.swing.JToolBar jToolBar1;
    private javax.swing.JMenuItem jmniClose;
    private javax.swing.JMenuItem jmniCopy;
    private javax.swing.JMenuItem jmniCut;
    private javax.swing.JMenuItem jmniLoad;
    private javax.swing.JMenuItem jmniPaste;
    private javax.swing.JMenuItem jmniSaveProject;
    private javax.swing.JMenuItem jmniSearch;
    private javax.swing.JMenuItem jmniUndo;
    private javax.swing.JMenu jmnuEdit;
    private javax.swing.JMenu jmnuFile;
    private javax.swing.JMenuItem jmnuRedo;
    // End of variables declaration//GEN-END:variables
    
}
